package com.cloudcode.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.cloudcode.dto.req.DocumentContentReq;
import com.cloudcode.enums.DocumentTypeEnum;
import com.cloudcode.service.DataEngine;
import com.cloudcode.service.SearchCoreService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @Author yunst
 * @data 2023/10/24 10:45
 */
@Slf4j
@Service
public class DataEngineImpl implements DataEngine {
    @Resource
    private SearchCoreService searchCoreService;

    @Override
    public void start() {
        handle("https://www.cnblogs.com/lytcreate/p/17784095.html");
    }

    @Override
    public void handle(String url) {
        try {
            DocumentContentReq documentContentReq = documentParse(url);
            searchCoreService.saveDocument(documentContentReq);
        } catch (IOException e) {
            e.printStackTrace();
            log.error("解析页面失败");
        }
    }

    /**
     * 文档解析
     * @param url
     * @return
     * @throws IOException
     */
    private static DocumentContentReq documentParse(String url) throws IOException {
        Document document = Jsoup.connect(url).get();
        // 提取标题
        String title = document.title();
        log.info("网页标题:{}", title);
        // 获取网页中的所有文本内容
        String allText = document.text();
        log.info("网页内容:{}", allText);
        // 获取所有图像标签
        Elements imgElements = document.select("img");
        // 遍历图像标签并获取src属性值
        List<String> imgArrayList = new ArrayList<>();
        for (Element imgElement : imgElements) {
            String imgSrc = imgElement.attr("src");
            log.info("图片地址:{}", imgSrc);
            imgArrayList.add(imgSrc);
        }
        DocumentContentReq documentContentReq = new DocumentContentReq();
        documentContentReq.setContent(allText);
        documentContentReq.setPath(url);
        documentContentReq.setTitle(title);
        documentContentReq.setImg(imgArrayList);
        documentContentReq.setType(DocumentTypeEnum.TEXT);
        return documentContentReq;
    }
}
